import Love from "../love/love"
import Road from "../obj/road"
import Car from "../obj/car"
import NpcCtrl from "../obj/ctrl"
import Item from "../obj/item"

var love = new Love("game")
love.enter = function (){
  love.graphics.setBackgroundColor("white")
  love.graphics.setDesignResolution(1600,900)
  this.dim = love.graphics.getDimensions()
  this.road = new Road(love)
  this.car = new Car(love)
  this.npcCtrl = new NpcCtrl(love,3,1)
  this.car.setToRoad(true,this.road,2,0)
  this.time = 0
  this.npc = []
  this.items = []
  this.score = 0
}
love.makeNPC= function(offset,carType){
  var car = new Car(love,love.math.randomInt(0,35))
  car.setToRoad(false,this.road,love.math.randomInt(1,5),this.car.tour+offset)
  this.npc.push(car)
}
love.makeItem = function(offset,itemType){
  var item = new Item(love,love.math.randomInt(0,10))
  item.setToRoad(this.road, love.math.randomInt(1, 5),this.car.tour+offset)
  this.items.push(item)
}

love.updateObj = function(name,dt){
  let tab = this[name]
  var newTab = []
  for (var i = 0; i < tab.length; i++) {
    var obj = tab[i]
    obj.update(dt)
    if (!obj.destroyed) {
      newTab.push(obj)
    }
  }
  this[name] = newTab
}
love.drawObj = function(name) {
  let tab = this[name]
  for (var i = 0; i < tab.length; i++) {
    let obj = tab[i]
    obj.draw()
  }
}

love.update = function(dt){
  this.time = this.time + dt
  this.car.update(dt)
  this.road.update(dt)
  this.npcCtrl.update(dt)
  this.updateObj("items",dt)
  this.updateObj("npc",dt)
  var newItem = []
}

love.drawUI = function(){
  love.graphics.setColor("black")
  love.graphics.setFont(love.loader.data.font40)
  love.graphics.print("HP "+ Math.floor(this.car.hp)+"/"+this.car.hpMax,20,40)
  love.graphics.print("FUEL " + Math.floor(this.car.fuel*10)/10+"/"+ this.car.fuelMax, 320, 40)
  love.graphics.print("N2O " + Math.floor(this.car.n2o) +"/"+this.car.n2oMax,620,40)
  love.graphics.print("SPEED " + Math.floor(this.car.speed*1000)/10 + "KM/H",920,40 )

  love.graphics.print("TOUR " + Math.floor(this.car.tour/3600)/10 + "KM",20,880)
  love.graphics.print("TIMER " + Math.floor(this.time / 100) / 10 + " S", 320, 880)
  love.graphics.print("COIN " + this.score + " PTS", 620, 880)
  /*
  love.graphics.print("COIN " + this.car.coin + " PCS",620,880)
  love.graphics.print("ITEMS ", 920, 880)
  love.graphics.setLineWidth(1)
  love.graphics.rectangle("stroke",1100,820,60,60)
  love.graphics.rectangle("stroke",1180,820,60,60)
  love.graphics.rectangle("stroke", 1260, 820, 60, 60)
  love.graphics.rectangle("stroke", 1340, 820, 60, 60)
  love.graphics.rectangle("stroke", 1420, 820, 60, 60)
  */
}

love.draw = function(){
  this.road.draw()
  this.car.draw()
  this.drawObj("items")
  this.drawObj("npc")
  love.drawUI()
}

love.touch.onTouch = function(touch){
  this.car.control(touch)
}
export default love